from app.extensions import db
from datetime import datetime
from app.models.model_mixin import BaseMixin


class Orders(db.Model, BaseMixin):
    id = db.Column(db.Integer, primary_key=True)
    nickname = db.Column(db.String(255))
    price = db.Column(db.String(255))
    email = db.Column(db.String(255))
    messages = db.Column(db.String(255))
    status = db.Column(db.Integer, default=10, index=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True)

    def to_dict(self, content=False):
        res_data = {
            'id': self.id,
            'nickname': self.nickname,
            'price':  self.price,
            'email': self.email,
            'messages': self.messages,
            'status': self.status,
            'created_at': self.created_at,
        }
        return res_data

    @staticmethod
    def to_collection_dict(query, page, per_page=10):
        resources = query.paginate(page, per_page, False)
        data = {
            'items': [item.to_dict() for item in resources.items]
        }
        return data
